﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Util;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using Dewin.BO.AgentMember;
using Log.Controller;
using Tabber.AgentControls;
using Tabber.Model;
using Tabber.Util;

namespace Tabber
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        #region Form
        public MainForm()
        {
            InitializeComponent();
            SetText();
            LoadAgentTab();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Gan ham delegate de sau moi lan selected value cua combobox language change thi change text
            cboLanguage.ReSetText = SetText;
        }

        /// <summary>
        /// Hoi truoc khi dong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(TabberCommon.GetString("ConfirmationContent"), TabberCommon.GetString("ConfirmationTitle"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// Khi form nay dong thi dong ca chuong trình
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            TabberCommon.CloseProgram();
        }

        /// <summary>
        /// Ham load thong tin agent len
        /// </summary>
        private void LoadAgentTab()
        {
            try
            {
                var list = Agent.GetAccounts();

                foreach (var agent in list)
                {
                    AddAgent(agent);

                    // Hien tai chi lay thang agent dau tien
                    break;
                }
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Tao tab va user control cua agent
        /// </summary>
        /// <param name="agent"></param>
        private void AddAgent(Agent agent)
        {
            try
            {
                var tab = new XtraTabPage();
                AgentAccountTab.TabPages.AddRange(new[] { tab });
                var mainControl = new UcMainAgent { Name = "UcMainAgent", AgentObj = agent, AgentId = agent.Id};
                tab.Controls.Add(mainControl);
                // Hien tai chi co 1 agent nen chua co nhut close
                tab.ShowCloseButton = DefaultBoolean.False;
                mainControl.Dock = DockStyle.Fill;
                tab.Name = String.Format("Tab{0}", agent.Id);
                tab.Text = agent.Id;
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Ham set text theo ngon ngu
        /// </summary>
        private void SetText()
        {
            try
            {
                Text = TabberCommon.GetString("MainForm");
                tabAgent.Text = TabberCommon.GetString("Agent");
                tabMember.Text = TabberCommon.GetString("Member");

                // Lay tung tab agent de thay doi text
                foreach (XtraTabPage tabPage in AgentAccountTab.TabPages)
                {
                    var mainControl = (UcMainAgent)tabPage.Controls["UcMainAgent"];
                    mainControl.SetText();
                }
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }
        #endregion

        #region Agent edit
        private void btnAddAgent_Click(object sender, EventArgs e)
        {

        }
        #endregion
    }
}